#include "DopplerGateItem.h"
//#include <QtGui>

#include <QGraphicsItem>
#include <QColor>
#include <QList>
#include <QVarLengthArray>
#include <QPainter>
#include <QStyleOptionGraphicsItem>

static int g_nZValue = 200 ;
DopplerGateItem::DopplerGateItem(const QColor& cColor_)
{
    m_nWidth  = 10;
    m_nHeight = 5;
    m_cColor   = cColor_;
    setZValue(g_nZValue++);

    setFlags(/*ItemIsPanel */ItemIsSelectable | ItemIsMovable);
    //setAcceptsHoverEvents(true);
}

QRectF DopplerGateItem::boundingRect() const
{
    int _nHeight = (m_nHeight + 3) / 2 ;
    return QRectF(0, - _nHeight , m_nWidth , 2 * _nHeight + 1);
}

QPainterPath DopplerGateItem::shape() const
{
    QPainterPath path;
    int _nHeight = (m_nHeight + 3) / 2 ;
    path.addRect(0, - _nHeight , m_nWidth , 2 * _nHeight + 1);
    return path;
}

void DopplerGateItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(widget); // to tell the compiler this parameter is not used, do not expose warning
    //******** to change the background color of when mouse is rect
    QColor fillColor = (option->state & QStyle::State_Selected) ? m_cColor.dark(150) : m_cColor;
    if (option->state & QStyle::State_MouseOver)
        fillColor = fillColor.light(125);
    //******** get the enlarged rate, and by which to determine whether draw the details
    //******** the real display size diliver real size (in pixels)
    //const qreal lod = option->levelOfDetailFromTransform(painter->worldTransform());
    QPen _OldPen = painter->pen();
    QPen _NewPen = _OldPen ;

    _NewPen.setWidth(1);
    _NewPen.setColor(fillColor);
    painter->setPen(_NewPen);

    int nTmp = 3 ;//(m_nHeight + 1 )/2 ;
    QVarLengthArray<QLineF, 2> lines;
    lines.append(QLineF(0 , - nTmp , 0 , nTmp ));
    lines.append(QLineF(m_nWidth , -nTmp , m_nWidth , nTmp));
    painter->drawLines(lines.data() , 2);
    _NewPen.setWidth(2);
    painter->setPen(_NewPen);
    painter->drawLine(QPointF(1 , 0) , QPointF(m_nWidth - 1 , 0));
    painter->setPen(_OldPen);
}

void DopplerGateItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    DopplerGraphicsItem::mousePressEvent(event) ;
    QGraphicsItem::mousePressEvent(event);
}

void DopplerGateItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseMoveEvent(event);
}
//
//void DopplerGateItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
//{
//    QGraphicsItem::mouseReleaseEvent(event);//
//
//}

